home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 May: Tool Chest / Dev.CD May 97 TC.toast / Sample Code / Snippets / QuickDraw / CopybitsSpeedPalette / Source / CopybitsSpeedPalette.c
Encoding:
C/C++ Source or Header  |  1996-09-17  |  9.6 KB  |  370 lines  |  [TEXT/CWIE]

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                            */
  3. /*        CopybitsSpeedPalette.                                                */
  4. /*            by John Wang                                                    */
  5. /*                                                                            */
  6. /*        Description:    This program demostrates ways to increase copybits    */
  7. /*            speed when using palettes.                                        */
  8. /*                                                                            */
  9. /*        Version:        1.0 Completed 11/12/91                                */
  10. /*                                                                            */
  11. /*--------------------------------------------------------------------------*/
  12.  
  13. #include     <Dialogs.h>
  14. #include    <Resources.h>
  15. #include    <Menus.h>
  16. #include    <Devices.h>
  17. #include    <ToolUtils.h>
  18. #include     <GestaltEqu.h>
  19. #include    <Palettes.h>
  20. #include    <QDOffscreen.h>
  21. #include     <Processes.h>
  22. #include    <OSUtils.h>
  23.  
  24. #define Gestalttest        0xA1AD
  25. #define NoTrap            0xA89F
  26.  
  27. #define    appleID            128            
  28. #define    appleMenu        0
  29. #define    aboutMeCommand    1
  30.  
  31. #define    fileID            129
  32. #define    quitCommand     1
  33.  
  34. #define    PictID            128
  35. #define    clutID            150
  36.  
  37. #define    aboutMeDLOG        128
  38. #define    okButton        1
  39.  
  40. /*------------------------------------------------------*/
  41. /*    Global Variables.                                    */
  42. /*------------------------------------------------------*/
  43.  
  44. Rect                TotalRect, minRect, WinMinusScroll, InitWindowSize;
  45. WindowPtr            myWindow;
  46. CTabHandle            mycolors;
  47. PaletteHandle        srcPalette;
  48. Boolean                DoneFlag;
  49. MenuHandle            mymenu0, mymenu1;
  50. PicHandle            ThePict;
  51. GWorldPtr            offscreenGWorld;
  52. enum PointSelector {topLeft, botRight};
  53.  
  54. void draw();
  55. void doCommand(long mResult);
  56. void showAboutMeDialog();
  57. void init();
  58.  
  59.  
  60. /*------------------------------------------------------*/
  61. /*    showAboutMeDialog().                                */
  62. /*------------------------------------------------------*/
  63.  
  64. void showAboutMeDialog()
  65. {
  66.     GrafPtr     savePort;
  67.     DialogPtr    theDialog;
  68.     short        itemHit;
  69.  
  70.     GetPort(&savePort);
  71.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  72.     SetPort(theDialog);
  73.  
  74.     do {
  75.         ModalDialog(nil, &itemHit);
  76.     } while (itemHit != okButton);
  77.  
  78.     CloseDialog(theDialog);
  79.  
  80.     SetPort(savePort);
  81.     return;
  82. }
  83.  
  84. /*------------------------------------------------------*/
  85. /*    init().                                                */
  86. /*------------------------------------------------------*/
  87.  
  88. void init()
  89. {
  90.     RgnHandle            tempRgn;
  91.     Rect                BaseRect;
  92.     OSErr                err;
  93.     long                QDfeature, OSfeature;
  94.     GDHandle            SaveGD;
  95.     CGrafPtr            SavePort;
  96.  
  97.     /*    Initialize Managaer.    */
  98.     InitGraf(&qd.thePort);
  99.     FlushEvents(everyEvent, 0);
  100.     InitWindows();
  101.     InitDialogs(nil);
  102.     InitCursor();
  103.  
  104.     /*    Set up menus.    */
  105.     mymenu0 = GetMenu(appleID);
  106.     AppendResMenu(mymenu0, 'DRVR');
  107.     InsertMenu(mymenu0,0);
  108.     mymenu1 = GetMenu(fileID);
  109.     InsertMenu(mymenu1,0);
  110.     DrawMenuBar();
  111.     DoneFlag = false;
  112.     ThePict = GetPicture(PictID);
  113.     if (ThePict == nil)
  114.         DoneFlag = true;
  115.  
  116.     /*    Use Gestalt to find is QuickDraw is avaiable.    */
  117.     if ((GetOSTrapAddress(Gestalttest) != GetOSTrapAddress(NoTrap))) {
  118.         err = Gestalt(gestaltQuickdrawVersion, &QDfeature);
  119.         if (err)
  120.             DoneFlag = true;
  121.         err = Gestalt(gestaltSystemVersion, &OSfeature);
  122.         if (err)
  123.             DoneFlag = true;
  124.         if (!DoneFlag && (QDfeature & 0x0f00) != 0x0200 && OSfeature < 0x0605)
  125.             DoneFlag = true;
  126.         }
  127.     else
  128.         DoneFlag = true;
  129.  
  130.     /*    Set Rects.    */
  131.     SetRect(&BaseRect, 40, 60, 472, 282);
  132.     SetRect(&WinMinusScroll, BaseRect.left-40, BaseRect.top-60, BaseRect.right-60, 
  133.                 BaseRect.bottom - 80);
  134.     SetRect(&InitWindowSize, WinMinusScroll.left, WinMinusScroll.top, 
  135.                             WinMinusScroll.right, WinMinusScroll.bottom);
  136.     tempRgn = GetGrayRgn();
  137.     HLock ((Handle) tempRgn);
  138.     TotalRect = (**tempRgn).rgnBBox;
  139.     SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
  140.                 (**tempRgn).rgnBBox.bottom - 40);
  141.     HUnlock ((Handle) tempRgn);
  142.  
  143.     /*    Open window and set up picture.    */
  144.     GetGWorld (&SavePort, &SaveGD);
  145.     mycolors = GetCTable (clutID);
  146.     (*mycolors)->ctFlags |= 0x4000;
  147.  
  148.     myWindow = NewCWindow(nil, &BaseRect, (ConstStr255Param)"", true, zoomDocProc, 
  149.                             (WindowPtr) -1, true, 150);
  150.     SetGWorld((CGrafPtr)myWindow, SaveGD);
  151.     DrawGrowIcon (myWindow);
  152.  
  153.     srcPalette = NewPalette (((**mycolors).ctSize)+1, mycolors,
  154.             pmTolerant + pmExplicit + pmAnimated, 0);
  155.     SetPalette ((WindowPtr) myWindow, srcPalette, true);
  156.     
  157.     GetGWorld (&SavePort, &SaveGD);
  158.     err = NewGWorld (&offscreenGWorld, 8, &InitWindowSize, mycolors, nil, 0);
  159.     if (err)
  160.         Debugger();
  161.     SetGWorld (offscreenGWorld, nil);
  162.     EraseRect (&InitWindowSize);
  163.     DrawPicture (ThePict, &InitWindowSize);
  164.     SetGWorld (SavePort, SaveGD);
  165. }
  166.  
  167. /*------------------------------------------------------*/
  168. /*    doCommand().                                        */
  169. /*------------------------------------------------------*/
  170.  
  171. void doCommand(mResult)
  172.     long    mResult;
  173. {
  174.     int                     theMenu, theItem;
  175.     Str255                    daName;
  176.     GrafPtr                 savePort;
  177.  
  178.     theItem = LoWord(mResult);
  179.     theMenu = HiWord(mResult);
  180.     
  181.     switch (theMenu) {
  182.         case appleID:
  183.             if (theItem == aboutMeCommand)
  184.                 showAboutMeDialog();
  185.             else {
  186.                 GetMenuItemText(mymenu0, theItem, daName);
  187.                 GetPort(&savePort);
  188.                 (void) OpenDeskAcc((ConstStr255Param)daName);
  189.                 SetPort(savePort);
  190.             }
  191.             break;
  192.  
  193.         case fileID:
  194.             switch (theItem) {
  195.                 case quitCommand:
  196.                     DoneFlag = true;
  197.                     break;
  198.                 default:
  199.                     break;
  200.                 }
  201.             break;
  202.     }
  203.     HiliteMenu(0);
  204.     return;
  205. }
  206.  
  207. /*------------------------------------------------------*/
  208. /*    draw.                                                */
  209. /*------------------------------------------------------*/
  210.  
  211. void draw()
  212. {
  213.     RGBColor        black = {0,0,0}, White = {65535,65535,65535};
  214.     long            before, delay;
  215.     //char theString[255];
  216.     GDHandle        screensDevice;
  217.     Rect            area;
  218.         
  219.     RGBForeColor (&black);
  220.     RGBBackColor (&White);
  221.     EraseRect(&WinMinusScroll);
  222.     
  223.     /* This is the only change made to support a faster copybits on one screen.
  224.         ctFlags is still set above. */
  225.     area = WinMinusScroll;
  226.     LocalToGlobal(topLeft);
  227.     LocalToGlobal((Point*)botRight);
  228.     screensDevice = GetMaxDevice(&area);
  229.     if (screensDevice != nil)
  230.         (**(**offscreenGWorld->portPixMap).pmTable).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  231.         
  232.     CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  233.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  234.     EraseRect(&WinMinusScroll);
  235.     CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  236.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  237.     EraseRect(&WinMinusScroll);
  238.     CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  239.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  240.     EraseRect(&WinMinusScroll);
  241.     CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  242.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  243.     EraseRect(&WinMinusScroll);
  244.     before = TickCount();
  245.     CopyBits ((BitMap *) *offscreenGWorld->portPixMap, &myWindow->portBits, 
  246.                     &InitWindowSize, &WinMinusScroll, srcCopy, nil);
  247.     delay = TickCount() - before;
  248. /*    DebugStr("\PIt's me!!!");    */
  249. }
  250.  
  251. /*------------------------------------------------------*/
  252. /*    main().                                                */
  253. /*------------------------------------------------------*/
  254.  
  255. main()
  256. {
  257.     char            key;
  258.     Boolean            track;
  259.     long            growResult;
  260.     EventRecord     myEvent;
  261.     WindowPtr        whichWindow;
  262.     int                yieldTime;
  263.  
  264.  
  265.     init();
  266.     yieldTime = 0;
  267.     for ( ;; ) {
  268.         if (DoneFlag)
  269.             ExitToShell();
  270.             
  271.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  272.             switch (myEvent.what) {
  273.                 case mouseDown:
  274.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  275.                         case inSysWindow:
  276.                             SystemClick(&myEvent, whichWindow);
  277.                             break;
  278.                         case inMenuBar:
  279.                             doCommand(MenuSelect(myEvent.where));
  280.                             break;
  281.                         case inContent:
  282.                             break;
  283.                         case inDrag:
  284.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  285.                             draw();
  286.                             DrawGrowIcon (whichWindow);
  287.                             break;
  288.                         case inGrow:
  289.                             growResult = GrowWindow (whichWindow, myEvent.where,
  290.                                                     &minRect);
  291.                             SizeWindow(whichWindow, LoWord(growResult), 
  292.                                     HiWord(growResult), true);
  293.                             EraseRect(&whichWindow->portRect);
  294.                             SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  295.                                     whichWindow->portRect.top, 
  296.                                     whichWindow->portRect.right-20, 
  297.                                     whichWindow->portRect.bottom - 20);
  298.                             draw();
  299.                             DrawGrowIcon (whichWindow);
  300.                             break;
  301.                         case inGoAway:
  302.                             track = TrackGoAway (whichWindow, myEvent.where);
  303.                             if (track) {
  304.                                 CloseWindow (whichWindow);
  305.                                 DoneFlag = true;
  306.                                 }
  307.                             break;
  308.                         case inZoomIn:
  309.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  310.                             if (track) {
  311.                                 ZoomWindow (whichWindow, inZoomIn, true);
  312.                                 EraseRect(&whichWindow->portRect);
  313.                                 SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  314.                                         whichWindow->portRect.top, 
  315.                                         whichWindow->portRect.right-20, 
  316.                                         whichWindow->portRect.bottom - 20);
  317.                                 draw();
  318.                                 DrawGrowIcon (whichWindow);
  319.                                 }
  320.                             break;
  321.                         case inZoomOut:
  322.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  323.                             if (track) {
  324.                                 ZoomWindow (whichWindow, inZoomOut, true);
  325.                                 EraseRect(&whichWindow->portRect);
  326.                                 SetRect(&WinMinusScroll, whichWindow->portRect.left, 
  327.                                         whichWindow->portRect.top, 
  328.                                         whichWindow->portRect.right-20, 
  329.                                         whichWindow->portRect.bottom - 20);
  330.                                 draw();
  331.                                 DrawGrowIcon (whichWindow);
  332.                                 }
  333.                             break;
  334.                         default:
  335.                             break;
  336.                         }
  337.                     break;
  338.                 case keyDown:
  339.                 case autoKey:
  340.                     key = myEvent.message & charCodeMask;
  341.                     if ( myEvent.modifiers & cmdKey )
  342.                         if ( myEvent.what == keyDown )
  343.                             doCommand(MenuKey(key));
  344.                     break;
  345.                 case updateEvt:
  346.                     if ((WindowPtr) myEvent.message == myWindow) {
  347.                         BeginUpdate((WindowPtr) myWindow);
  348.                         EndUpdate((WindowPtr) myWindow);
  349.                         draw();
  350.                         }
  351.                     break;
  352.                 case diskEvt:
  353.                     break;
  354.                 case activateEvt:
  355.                     break;
  356.                 case 15:
  357.                     if ((myEvent.message << 31) == 0) {
  358.                         yieldTime = 30;
  359.                         }
  360.                     else {
  361.                         yieldTime = 0;
  362.                         SetPort((WindowPtr) myWindow);
  363.                         }
  364.                     break; 
  365.                 default:
  366.                     break;
  367.                 }
  368.             }
  369.         }
  370. }